package com.demo.java.rxjava;

import com.demo.java.utils.ThreadUtils;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

import static com.demo.java.utils.LogUtils.logt;

/**
 * Rxjava异步处理
 */
public class RxJavaObserveTest {
    public static void main(String[] args) {

        RxJavaObserveTest test = new RxJavaObserveTest();
        test.asyncCallback(new Observer<String>() {
            @Override
            public void onSubscribe(@NonNull Disposable disposable) {
                System.out.println(logt("on subscribe"));
            }

            @Override
            public void onNext(@NonNull String s) {
                System.out.println(logt("on next, " + s));
            }

            @Override
            public void onError(@NonNull Throwable throwable) {
                System.out.println(logt("run error"));
            }

            @Override
            public void onComplete() {
                System.out.println(logt("on complete"));
            }
        });

        ThreadUtils.sleep(5);

    }

    public void asyncCallback(Observer<String> observer) {
        new Thread(() -> {

            Observable.just("a", "b").subscribe(observer);
        }, "async-thread").start();
    }

}
